using System.Collections.Generic;
using abilities.effects;
using abilities.templates;
using abilities.triggers;
using UnityEngine;

namespace abilities
{
    // 技能管理器挂在英雄上，执行能力/被动攻击，设置所有触发器
    public class AbilityManager:MonoBehaviour
    {
        public UnitObject Owner { get; private set; }
        List<Effect> effects = new();

        void Awake()
        {
            this.Owner = GetComponent<UnitObject>();
        }

        // 使用所有技能。
        public void ApplyAbilities(List<Ability> abilities, int level)
        {
            if (abilities.Count < 1)
            {
                return;
            }

            //效果。
            foreach (var it in abilities)
            {
                if (it.effect != null)
                {
                    Apply(it.effect, level);
                }
            }
        }

        //移除时
        public void OnRemoveFromLocation()
        {
            foreach (var it in effects)
            {
                it.StopTriggers();
            }
        } 
        //应用
        public void Apply(EffectTemplate effect, int level)
        {
            Debug.Log("apply");

            // 无效的
            if (effect.stackType == AbilityEffectStackType.NonStackable && HasEffectsByPrototype(effect))
                return;
            if (effect.stackType == AbilityEffectStackType.Replaceable)
            {
                CancelEffectsByPrototype(effect);
            }

            Effect effectinstance = effect.CreateInstance(level);
            effects.Add(effectinstance);
            effectinstance.StartTriggers(new AbilityContext(effectinstance, this));
        }

        //取消
        public void Cancel(Effect effect)
        {
            Debug.Log("cancel");

            for (int i = effects.Count - 1; i >= 0; i--)
            {
                var it = effects[i];
                if (effect == it)
                {
                    it.CancelEffect(Owner);
                    it.ReleaseTriggers();
                    effects.RemoveAt(i);
                    break;
                }
            }
        }

        void CancelEffectsByPrototype(EffectTemplate prototype)
        {
            if (prototype == null)
                return;

            List<Effect> cancelEffects = new();
            foreach (var it in effects)
            {
                if (prototype == it.prototype)
                {
                    cancelEffects.Add(it);
                }
            }

            foreach (var iter in cancelEffects)
            {
                Cancel(iter);
            }
        }

        bool HasEffectsByPrototype(EffectTemplate prototype)
        {
            if (prototype == null)
                return false;

            foreach (var it in effects)
            {
                if (prototype == it.prototype)
                {
                    return true;
                }
            }

            return false;
        }

        // 死亡过程中前
        bool OnBeforeDeath()
        {
            return ActivateEffect(AbilityTriggerType.OnDeath);
        }

        // 攻击时
        public bool OnAttack()
        {
            return ActivateEffect(AbilityTriggerType.OnAttack);;
        }

        // 受击时
        public void OnHit()
        {
            ActivateEffect(AbilityTriggerType.OnHit);
        }

        bool ActivateEffect(AbilityTriggerType triggerType)
        {
            bool result = false;
            foreach (var it in effects)
            {
                result = result || it.ActivateTriggers(triggerType);
            }
            return result;
        }
    }
}